package com.zone.mybatis.type;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: zongzi
 * @description: TODO
 * @date: 2023/12/21
 */
public class TypeAliasRegistry {
    private final Map<String, Class<?>> TYPE_ALIASES = new HashMap<>();

    public TypeAliasRegistry() {
        registerAlias("byte", Byte.class);
        registerAlias("short", Short.class);
        registerAlias("int", Integer.class);
        registerAlias("integer", Integer.class);
        registerAlias("long", Long.class);
        registerAlias("float", Float.class);
        registerAlias("double", Double.class);
        registerAlias("boolean", Boolean.class);
        registerAlias("char", Character.class);
        registerAlias("string", String.class);
    }

    public void registerAlias(String alias, Class<?> type) {
        TYPE_ALIASES.put(alias, type);
    }

    @SuppressWarnings("unchecked")
    public <T> Class<T> getType(String alias) {
        Class<?> type = TYPE_ALIASES.get(alias);
        if (type == null) {
            throw new RuntimeException("unknown alias: " + alias);
        }
        return (Class<T>) type;
    }
}
